//
//  Email.h
//  YuchBox
//
//  Created by Ivan on 11-10-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "StreamPeer.h"
#import "Attachment.h"
#import "IvanSqliteDB.h"

#define EMAIL_SEND_STYLE_NOTHING    0
#define EMAIL_SEND_STYLE_FORWORD    1
#define EMAIL_SEND_STYLE_REPLY      2

#define EMAIL_STATUS_RECV_READ           0
#define EMAIL_STATUS_RECV_UNREAD         1
#define EMAIL_STATUS_RECV_ATTACH_READ    2
#define EMAIL_STATUS_RECV_ATTACH_UNREAD  3
#define EMAIL_STATUS_SEND_SENDING        4
#define EMAIL_STATUS_SEND_SENT           5
#define EMAIL_STATUS_SEND_FAIL           6
#define EMAIL_STATUS_SEND_DRAFT          7

// 
// @class Email
//
@class EmailAttachment;

@interface Email : NSObject
{
    NSInteger       _index;                           // receive index(given by the server)
    UInt64          _uid;                             // unique id. fetch from the sqlite database
    UInt64          _accountUid;                        
    
    NSUInteger      _hashCode;
    NSInteger       _version;                         //
    
    NSMutableArray  *_fromArray;                      //
    NSMutableArray  *_replyToArray;                   //
    NSMutableArray  *_ccToArray;                      //
    NSMutableArray  *_bccToArray;                     //
    NSMutableArray  *_toArray;                        //
    NSMutableArray  *_groupArray;                     //
    
    NSString        *_subject;                        //
    NSString        *_xmailName;                      // 
    NSString        *_content;                        //
    NSString        *_contentHtml;                    //
    
    NSDate          *_sendDate;                       //
    
    UInt64          _sendTimestamp;                   //
    NSInteger       _flag;                            // send 0
    
    NSInteger       _status;                          // read / unread / attached unread / attached read / send
    
    NSMutableArray  *_attachmentArray;                // attachment: file, photo and so on
    Boolean         _hasLocationInfo;                    
        
    // for version >= 2
    NSString        *_contentHtmlType;                //
    
    
    // other
    UInt8           _sendStyle;                       // nothing(general send)/forword mail/reply mail
    Boolean         _doWannaSave;                     // dose want to save the sent mail
    
}

@property (assign, nonatomic) NSInteger         index;
@property (assign, nonatomic) UInt64            uid;
@property (assign, nonatomic) UInt64            accountUid;
//@property (assign, nonatomic) NSUInteger        hashCode;     // do not get and set obviously
@property (assign, nonatomic) NSInteger         version;

@property (retain, nonatomic) NSMutableArray    *fromArray;
@property (retain, nonatomic) NSMutableArray    *replyToArray;
@property (retain, nonatomic) NSMutableArray    *ccToArray;
@property (retain, nonatomic) NSMutableArray    *bccToArray;
@property (retain, nonatomic) NSMutableArray    *toArray;
@property (retain, nonatomic) NSMutableArray    *groupArray;

@property (copy,   nonatomic) NSString          *fromString;
@property (copy,   nonatomic) NSString          *replyToString;
@property (copy,   nonatomic) NSString          *ccToString;
@property (copy,   nonatomic) NSString          *bccToString;
@property (copy,   nonatomic) NSString          *toString;
@property (copy,   nonatomic) NSString          *groupString;

@property (copy,   nonatomic) NSString          *subject;
@property (copy,   nonatomic) NSString          *xmailName;
@property (copy,   nonatomic) NSString          *content;
@property (copy,   nonatomic) NSString          *contentHtml;

@property (retain, nonatomic, setter = setSendDate:) NSDate            *sendDate;
@property (assign, nonatomic, setter = setSendTimestamp:) UInt64        sendTimestamp;

@property (retain, nonatomic) NSMutableArray    *attachmentArray;

@property (assign, nonatomic) NSInteger         flag;
@property (assign, nonatomic) NSInteger         status;

@property (assign, nonatomic) Boolean           hasLocationInfo;

@property (copy,   nonatomic) NSString          *contentHtmlType;

@property (assign, nonatomic) UInt8             sendStyle;
@property (assign, nonatomic) Boolean           doWannaSave;

- (id)initWithInputStream:(NSInputStream *)inStream;
- (void)outputMail:(NSOutputStream *)outStream;

- (NSUInteger)getHashCode;


- (void)save:(IvanSqliteDB *)con;
- (void)chagneStatusToRead:(IvanSqliteDB *)con;
- (void)changeStatus:(NSInteger)status con:(IvanSqliteDB *)con;
// static method
+ (NSInteger)retrieveEmails:(IvanSqliteDB *)con emails:(NSMutableArray *)emails limit:(NSInteger)limit offset:(NSInteger)offset;
+ (Email *)retrieveEmail:(IvanSqliteDB *)con pkId:(UInt64)pkId;
+ (NSInteger)getEmailTotal:(IvanSqliteDB *)con;
+ (NSInteger)getEmailUnreadTotal:(IvanSqliteDB *)con;

@end

//@interface Email (private) {


//@end